﻿package
{
		//color picker
	import fl.controls.ColorPicker;
    import fl.events.ColorPickerEvent;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import org.papervision3d.events.FileLoadEvent;
    import org.papervision3d.lights.PointLight3D;
    import org.papervision3d.materials.shadematerials.GouraudMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.materials.BitmapFileMaterial;
    import org.papervision3d.materials.shadematerials.PhongMaterial;
    import org.papervision3d.objects.parsers.DAE;
    import org.papervision3d.view.BasicView;
    import org.papervision3d.view.stats.StatsView;

    import fl.events.ColorPickerEvent;
	import org.papervision3d.materials.shadematerials.GouraudMaterial;

//set line3D
	import flash.system.System;
	import flash.display.*;
	import org.papervision3d.cameras.Camera3D;
  	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Line3D;
 	import org.papervision3d.core.geom.renderables.Vertex3D;
    import org.papervision3d.core.proto.MaterialObject3D;
    import org.papervision3d.materials.WireframeMaterial;
    import org.papervision3d.materials.special.LineMaterial;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.objects.primitives.Sphere;
    import org.papervision3d.render.BasicRenderEngine;
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.view.Viewport3D;
	import flash.events.*;
	import flash.filters.GlowFilter;
	import org.papervision3d.events.*;
    import org.papervision3d.objects.parsers.DAE;
	import org.papervision3d.objects.parsers.KMZ;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapViewportMaterial;
	import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.view.BitmapViewport3D;
	import org.papervision3d.core.math.Matrix3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.math.Number3D;

 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class BitmapViewport extends Sprite
	{	
		private var dae1:DisplayObject3D;
		private var dae2:DisplayObject3D;
		private var dae3:DisplayObject3D;
		private var dae4:DisplayObject3D;
		private var dae5:DisplayObject3D;
		private var dae6:DisplayObject3D;
		private var dae7:DisplayObject3D;
		private var dae8:DisplayObject3D;
		private var dae9:DisplayObject3D;
		private var dae10:DisplayObject3D;
		//set nguyen thuy 
		var mmat = new BitmapFileMaterial("MitLancerMap.jpg",true);
		private var materials:MaterialsList;
		
		private var colorPicker:ColorPicker;
		private var mat:WireframeMaterial;
		private var light:PointLight3D = new PointLight3D();

		//render
		private var renderer:BasicRenderEngine = new BasicRenderEngine();
		private var scene: Scene3D = new Scene3D();
		private var viewport: Viewport3D = new Viewport3D();
		private var camera: Camera3D = new Camera3D();
		
		//set gia tri chuot
		private var mouseDown:Boolean = false;
		
		//set mesh center
		
		
		private var viewport2:BitmapViewport3D;
		private var scene2:Scene3D = new Scene3D();
		private var sphere:Sphere;
		//set gia tri embed
		
		
		private var plane:Plane;
 		private var model:DisplayObject3D= new DisplayObject3D();
		
		
		private var maxX :Number = 0;
		private var minX :Number = 0;
		private var maxY :Number = 0;
		private var minY :Number = 0;
		private var maxZ :Number = 0;
		private var minZ :Number = 0;
		
		public function removeEvent():void{
				removeEventListener(Event.ENTER_FRAME,enter);
		}
		public function onUp(evt:MouseEvent):void{
				removeEventListener(Event.ENTER_FRAME,enter);
			}
		public function onDown(evt:MouseEvent):void{
				addEventListener(Event.ENTER_FRAME,enter);
			}
		public function BitmapViewport(url:String)
		{	
		addChild(viewport);
		this.addEventListener(MouseEvent.MOUSE_UP, onUp);
		this.addEventListener(MouseEvent.MOUSE_DOWN, onDown);

	
			load(url);
			
		}
		public function orgModel():void{
			var color :uint = Math.random() * 0xFFFFFF;
			mat = new WireframeMaterial(color,.5);
			model.replaceMaterialByName(mat,"all");
			model.setChildMaterial(dae1,mat);
			model.setChildMaterial(dae3,mat);
			model.setChildMaterial(dae2,mat);
			model.setChildMaterial(dae4,mat);
			}
		public function load(url:String):void{
		
			viewport.interactive=true;
			viewport.containerSprite.filters = [new GlowFilter(0xcc0000, 1, 10, 10, 2)]; //adds a red outline to the plane
 			camera.y=150;
			model= new DAE();
			var color :uint = Math.random() * 0xFFFFFF;
			mat = new WireframeMaterial(color,.5);
			materials= new MaterialsList();
			materials.addMaterial(mat,"all");
			DAE(model).load(url,materials);
			//will be rendered inside of the plane
			viewport2 = new BitmapViewport3D();
			viewport2.interactive=true;
			
			var light:PointLight3D = new PointLight3D();
			var sphereMaterial:FlatShadeMaterial = new FlatShadeMaterial(light, 0x00ffff);
			sphere = new Sphere(sphereMaterial, 400, 20, 20);

			//scene2.addChild(axes);
			model.addEventListener(FileLoadEvent.LOAD_COMPLETE, colladacomplete);
 			model.scale=100;
			scene2.addChild(model);
			//create a material of the above scene^^^
			var material:BitmapViewportMaterial = new BitmapViewportMaterial(viewport2, true);
			material.doubleSided = true;
 			material.interactive=true;
			plane = new Plane(material);
			scene.addChild(plane);
			plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,clickHandler);
 			addEventListener(Event.ENTER_FRAME,enter);
			
			//startRendering();
			}
		public function clickHandler(e:InteractiveScene3DEvent){
		}
			
		
		private function colladacomplete(e:Event):void {
			model.removeEventListener(FileLoadEvent.LOAD_COMPLETE,colladacomplete);
			dae1 = model.getChildByName("khung",true);
			dae2 = model.getChildByName("duoi",true);
			dae3 = model.getChildByName("kieng_left",true);
			dae4 = model.getChildByName("kieng_right",true);
			dae5 = model.getChildByName("banhtruoc_left",true);
			dae6 = model.getChildByName("banhtruoc_right",true);
			dae7 = model.getChildByName("banhsau_left",true);
			dae8 = model.getChildByName("banhsau_right",true);
			dae9 = model.getChildByName("default_matden",true);
			dae10 = model.getChildByName("default_material",true);
			parseMesh(model, new Number3D());
			
			var sizeX = maxX - minX;
			var sizeY = maxY - minY;
			var sizeZ = maxZ - minZ;
			
			var middleinx:int = ((minX + maxX)) / 2;
			if (DAE(model).yUp == false) {
	                 model.position = new Number3D( -middleinx, -minZ, -minY); 
					 if ( sizeZ > sizeX) {					 
								model.scale = 1400 / sizeZ;
									}
					 else {
						model.scale = 1400 / sizeX
						
						}
					 		}
	               else {
					model.position = new Number3D( -middleinx, -minY, -minZ);
						if (sizeY > sizeX) {
						model.scale = 1400 / sizeZ;
						}
						else {
						model.scale = 1400 / sizeX;	
							}
				        }
	}
 
 	private function parseMesh(obj:DisplayObject3D, pos:Number3D):void
	{
		if (obj.children)
		{
			for each( var child:* in obj.children )
			{
				var n:Number3D = new Number3D(
				pos.x + obj.x,
				pos.y + obj.y,
				pos.z + obj.z);
				parseMesh( child, n );
			}
		}

		if (obj.geometry)
		{
			for each(var vertex:Vertex3D in obj.geometry.vertices)
			{
				var vx:Number = pos.x + vertex.x;
				var vy:Number = pos.y + vertex.y;
				var vz:Number = pos.z + vertex.z;
		
				maxX = Math.max(maxX, vx);
				minX = Math.min(minX, vx);
				maxY = Math.max(maxY, vy);
				minY = Math.min(minY, vy);
				maxZ = Math.max(maxZ, vz);
				minZ = Math.min(minZ, vz);
		
			}
		}
}

	private function updateMaterials():void
        {	//mat.removeAllMaterials();
			model.replaceMaterialByName(mmat,"all");
			//var color:ColorMaterial = new ColorMaterial(0x000000);
            var mc:GouraudMaterial = new GouraudMaterial(light, colorPicker.selectedColor, 0x000000);
            model.setChildMaterial(dae1,mc);
			model.setChildMaterial(dae3,mc);
			model.setChildMaterial(dae2,mc);
			model.setChildMaterial(dae4,mc);
			//model.setChildMaterial(dae9,color);
			//model.setChildMaterial(dae10,color);
     }
	private function changeHandler(event:ColorPickerEvent):void {
            colorPicker.removeEventListener(ColorPickerEvent.CHANGE, changeHandler);
			updateMaterials();
        }
//set change picker color
	public function changeColor():void{
			colorPicker = new ColorPicker();
			colorPicker.scaleX=.5;
			colorPicker.scaleY=.5;
           	addChild(colorPicker);
			colorPicker.visible=false;
			colorPicker.x = 270;
			colorPicker.selectedColor = 0x00ff00;
			colorPicker.open();
            colorPicker.addEventListener(ColorPickerEvent.CHANGE, changeHandler);

			}
		//set material nguyen thuy
	 public function unWrapMap():void{
		 
			mmat.tiled = true;
			mmat.smooth = true;
		// model.replaceMaterialByName(mmat,"all");
		 if( materials)
			{
				if( materials is Array )
				{
					for( var i:String in materials )
					{
						delete this.materials[i];
					}
				}
				else if( materials is Object )
				{
					for( var name:String in materials )
					{
						delete this.materials[ name ];
					}
				}
			}
		model.replaceMaterialByName(mmat,"all");
	}
		public function remove():void{
			viewport2.lastRenderList.length = 0;
			//model.dispose();
			scene2.removeChild(model);
			model = null;
				}
		public function enter(evt:Event):void{
			model.rotationY+=2;
			renderer.renderScene(scene, camera, viewport);
			//render the main scene
			renderer.renderScene(scene2, camera, viewport2);
			}
	}
}